/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots;

/**
 * @author Xing Jiankuan
 *
 */


public class RTUP {
	private long rtu;// the number of replica turning from unused to used
	
    private long used;
    private long estimatedNum;
    private long addedCount;
    private long deletedCount;
    private long delDeleteCount;
	private static double e = 0.00000001; 
	
	
	public RTUP() {
		
	}

	public synchronized void addRtu(long times) {
		rtu += times;
	}


	public  synchronized void setUnused(RTUP lastRecord,
			long estimatedNum,long addCountByType, long deletedCountByType)
	{
		this.addedCount = addCountByType;
		this.deletedCount = deletedCountByType;
		this.estimatedNum = estimatedNum;
		if(lastRecord == null)
		{
			used = addCountByType;
			delDeleteCount = this.deletedCount - 0;
		}else
			if(this.addedCount >= lastRecord.addedCount &&
					this.deletedCount >= lastRecord.deletedCount)
		{
			used = lastRecord.used + 
			         this.addedCount - lastRecord.addedCount
			         - lastRecord.delDeleteCount;
			
			delDeleteCount = this.deletedCount - lastRecord.deletedCount;
		}
	}
	/**
	 * 
	 * @return Rtup value according formula
	 * @author Xing Jiankuan
	 */
	

	

	public double getValue() {
	    long unused = this.estimatedNum - this.used;
	    if(unused < 0)
	    {
	    	System.out.print("Error!!");
	    }
		double proportion
		= (rtu  + e)/ 
		  (unused + rtu  + 2*e);
		return proportion;
	}

	public String toString() {
		String temp = " rtu : " + this.rtu 
				+ " unused :  " + (estimatedNum - used);
		return temp;
	}

	public long getUsed() {
		
		return  this.used;
	}

}
